package util;

import java.lang.reflect.ParameterizedType;

/**
 * Created by 刘智强 on 2017/4/6.
 * 关于泛型的工具类
 */

public class TUtil {
    public static <T> T getT(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass().getGenericSuperclass())).getActualTypeArguments()[i]).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static <T> T getClazz(Object o, int i) {
        ParameterizedType type = (ParameterizedType) o.getClass().getGenericSuperclass();
        try {
            return ((Class<T>) (type.getActualTypeArguments()[i])).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;


    }


}
